package poweroftwo;

import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReference;

public class PowerOfTwo {
	
	private BigInteger initValue = new BigInteger("1");
	private AtomicReference<BigInteger> atref = new AtomicReference<BigInteger>(initValue);
	
	public BigInteger next() {
		BigInteger oldValue;
		BigInteger newValue;
		BigInteger bigTwo = new BigInteger("2");
		
		do {
		 oldValue = atref.get();
		 newValue = bigTwo.multiply(oldValue);
		} while (!atref.compareAndSet(oldValue, newValue));
		return newValue;
	}	
}
